﻿using System.Web.Http;
using System.Web.Http.Description;
using AttributeRouting.Web.Http.WebHost;
using HttpController.HttpHandler;
using HttpController.Override;
using System.Web.Http.Tracing;
using System.Web.Http.Controllers;
using HttpController.Extensions;

[assembly: WebActivator.PreApplicationStartMethod(typeof(HttpController.Bootstrap), "Start")]

namespace HttpController
{
    public static class Bootstrap
    {
        static void RegisterRoutes(HttpRouteCollection routes)
        {
            // See http://github.com/mccalltd/AttributeRouting/wiki for more options.
            // To debug routes locally using the built in ASP.NET development server, go to /routes.axd

            routes.MapHttpAttributeRoutes();

            GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpFormatter());

            /// 替换默认的IDocumentationProvider,从而可以在Help页面中查看具体API的含义
            System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(IDocumentationProvider),
                new XmlCommentDocumentationProvider(System.AppDomain.CurrentDomain.BaseDirectory + @"bin\HttpController.xml"));

            //System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(ITraceWriter),
            //    new PerRequestTraceWriter());

            /// 重写这句后，在APIController的ACTION中可以用int? id这种写法，如果不替换，默认情况下会报匹配不到路由，这点跟MVC不一样
            System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector),
                new HybridActionSelector());
        }

        public static void Start()
        {
            RegisterRoutes(GlobalConfiguration.Configuration.Routes);

            GlobalConfiguration.Configuration.MessageHandlers.Add(new TraceHelperHandler());
            //GlobalConfiguration.Configuration.MessageHandlers.Add(new HttpMethodOverrideHandler());
        }
    }
}